package tbs.littlebaby;

import AppSystem.AppUtils;
import AppSystem.Settinger;
import Data.BasicData;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class Setting extends AppCompatActivity {
    void SetText(int id,String text)
    {
        TextView textView=findViewById(id);
        textView.setText(text);
    }


    void MakeDataText(int rid)
    {
        TextView textView= findViewById(R.id.data_text);
      BasicData[] datas= MainActivity.save.Read(rid);
      for(int i=0;i<datas.length;i++)
      textView.setText(textView.getText()+datas[i].toString()+"\n");
    }

    @Override
    protected void onResume() {
        if(Settinger.isAutoread())
        {
            String text= AppUtils.getClipBoardText(this);
            if(text!=null&&text.length()>0) {
                String[] raw = text.split("\n");
                BasicData[] datas = ReadFromText(raw);
                Toast.makeText(this, String.format("共有%d行数据，成功读取%d行", raw.length, datas.length), Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this,"空剪切板",Toast.LENGTH_SHORT).show();
            }
        }

        super.onResume();
    }
    BasicData[] ReadFromText(String[] raw)
    {
        ArrayList<BasicData> result=new ArrayList<>();
        int t=0;
        for(int i=0;i<raw.length;i++)
        {
            try {
                result.add(BasicData.StringToData(raw[i]));
                t++;
            }catch (Exception ex)
            {
            }

        }
        BasicData[] res=new BasicData[result.size()];
        result.toArray(res);

        return  res;
    }
    public void InputClick(View view)
    {
        String text= AppUtils.getClipBoardText(this);
        if(text!=null&&text.length()>0)
        {
            String[] raw= text.split("\n");
            BasicData[] datas=ReadFromText(raw);
            int t=0;
            for (int i=0;i<datas.length;i++)
            {
              if(  MainActivity.save.Add(datas[i]))
              {
                  t++;
              }
            }

            RegetAllData();
            Toast.makeText(this,String.format("共有%d行数据，成功读取并导入%d行",raw.length,t),Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this,"空剪切板",Toast.LENGTH_SHORT).show();
        }

    }
    public void OutputClick(View view)
    {
        TextView view1=findViewById(R.id.data_text);
        AppUtils.setClipBoardText(this,view1.getText().toString());
    }
    void RegetAllData()
    {
        TextView textView= findViewById(R.id.data_text);
        textView.setText("");
        MakeDataText(R.string.Tag_cloth);
        MakeDataText(R.string.Tag_huazhuang);
        MakeDataText(R.string.Tag_pack);
        MakeDataText(R.string.Tag_shoe);
        MakeDataText(R.string.Tag_shoushi);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        SeekBar bar=findViewById(R.id.imageScaler);
        int n=(int) (Settinger.getImageScale()*10);
        Switch isauto=findViewById(R.id.autoread);
        isauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Settinger.setAutoread(isChecked);
                Settinger.SaveSetting(Setting.this);
            }
        });

        SetText(R.id.textView,"图片展示缩放:"+String.valueOf(n));
        RegetAllData();
        isauto.setChecked(Settinger.isAutoread());
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Settinger.setImageScale((float)progress/(float) 10);
                Settinger.SaveSetting(Setting.this);
                SetText(R.id.textView,"图片展示缩放:"+String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        isauto=findViewById(R.id.lockImage);
        isauto.setChecked(Settinger.isLockImage());
        isauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Settinger.setLockImage(isChecked);
                Settinger.SaveSetting(Setting.this);
            }
        });
        bar.setProgress(n);
    }
}